home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Acere (PowerPlant, Game) 1.2
/
AcereÄ.sit
/
Acereƒ
/
Code
/
WellStack.cp
< prev
next >
Wrap
Text File
|
1995-03-04
|
9KB
|
412 lines
// ===========================================================================
// WellStack.cp ⌐1993 Metrowerks Inc. All rights reserved.
// ===========================================================================
//
// Class for an object that can draw itself and respond to mouse clicks
#ifdef PowerPlant_PCH
#include PowerPlant_PCH
#endif
#include "WellStack.h"
#include "CardDeck.h"
#include "CardWell.h"
#include "AcereApp.h"
#include "CTextDoc.h"
#include <LView.h>
#include <LStream.h>
#include <PP_Messages.h>
#include <UDrawingState.h>
extern CTextDoc *theDoc;
extern CardDeck *theDeck;
extern unsigned long ourAvailRam;
// ---------------------------------------------------------------------------
// Ñ CreatePaneStream [static]
// ---------------------------------------------------------------------------
// Return a new Pane object initialized using data from a Stream
WellStack*
WellStack::CreateWellStack(
LStream *inStream)
{
return (new WellStack(inStream));
}
// ---------------------------------------------------------------------------
// Ñ WellStack()
// ---------------------------------------------------------------------------
// Default Constructor
WellStack::WellStack() : CardWell()
{
InitCard();
}
// ---------------------------------------------------------------------------
// Ñ WellStack(const WellStack&)
// ---------------------------------------------------------------------------
// Copy Constructor
WellStack::WellStack(const WellStack &inOriginal) : CardWell(inOriginal)
{
InitCard();
}
// ---------------------------------------------------------------------------
// Ñ WellStack(SPaneInfo&)
// ---------------------------------------------------------------------------
// Construct Pane from data in a struct
WellStack::WellStack(const SPaneInfo &inPaneInfo) : CardWell(inPaneInfo)
{
InitCard();
}
// ---------------------------------------------------------------------------
// Ñ WellStack(LStream*)
// ---------------------------------------------------------------------------
// Construct Pane from data in a Stream
WellStack::WellStack(
LStream *inStream) : CardWell(inStream)
{
InitCard();
}
// ---------------------------------------------------------------------------
// Ñ InitPane
// ---------------------------------------------------------------------------
// Initialize Pane from data in a struct
void
WellStack::InitCard()
{
itsCards = nil;
InitCards();
}
// ---------------------------------------------------------------------------
// Ñ ~WellStack
// ---------------------------------------------------------------------------
// Destructor
WellStack::~WellStack()
{
PutInside(nil);
if (sLastPaneClicked == this)
{
sLastPaneClicked = nil;
}
/* if (itsCards != nil)
{
PlayingCard *currentCard;
short i, k;
k = itsCards->GetCount();
for (i=1;i<=k; i++)
{
itsCards->FetchItemAt(i, ¤tCard);
delete currentCard;
}
delete itsCards;
}
itsCards = nil;
if (itsCard != nil)
delete itsCard;
*/
}
// ---------------------------------------------------------------------------
// Ñ Draw
// ---------------------------------------------------------------------------
// Try to draw contents of a Pane
//
// inSuperDrawRgnH specifies, in Port coordinates, the portion of the
// Pane's SuperView that needs to be drawn. Specify nil to bypass
// the intersection test.
//
// This is a wrapper function which calls DrawSelf if it is proper for
// the Pane to draw. This means that:
// > Pane's Visible property is on
// > Pane can be focused
// > Pane's Frame is in QuickDraw space
// > Pane's Frame intersects inSuperDrawRgnH
void WellStack::Draw(RgnHandle inSuperDrawRgnH)
{
Rect frame/*, useFrame */;
if ( IsVisible() &&
FocusDraw() &&
CalcPortFrameRect(frame) &&
((inSuperDrawRgnH == nil) || RectInRgn(&frame, inSuperDrawRgnH)) )
{
DrawSelf();
}
// DisposHandle((Handle)currentCard);
}
void WellStack::DrawSelf()
{
Rect frame, useFrame;
short i, maxItems;
/* RgnHandle saveClipRgn, ourRectRgn;
CalcPortFrameRect(frame);
saveClipRgn = NewRgn(); // get an empty region
GetClip( saveClipRgn ); // save current
ourRectRgn = NewRgn();
RectRgn(ourRectRgn, &frame);
SetClip( ourRectRgn ); // install desired clipRgn
*/
CalcPortFrameRect(frame);
// PlayingCard **currentCard = (PlayingCard **) NewHandle(4L);
PlayingCard *currentCard;
if ((itsCard == nil) || (itsCards == nil))
maxItems = 0;
else if (itsCard->card == kNoCard)
maxItems = 0;
else
maxItems = (short) itsCards->GetCount();
CalcPortFrameRect(frame);
if (maxItems == 0)
{
useFrame = frame;
useFrame.bottom = useFrame.top + (theDeck->GetCardHeight());
EraseRect(&useFrame);
FrameRoundRect(&useFrame, 20, 20);
}
for (i=1; i<= maxItems; i++)
{
itsCards->FetchItemAt(i, ¤tCard);
if (i == maxItems)
{
useFrame = frame;
useFrame.top = useFrame.bottom - (theDeck->GetCardHeight());
}
else
{
useFrame = frame;
useFrame.top = frame.top + (kStubHeight * (i-1));
useFrame.bottom = useFrame.top + kStubHeight;
}
if (currentCard == nil)
{
EraseRect(&frame);
FrameRoundRect(&useFrame, 20, 20);
}
else if (currentCard->card == kNoCard)
{
EraseRect(&frame);
FrameRoundRect(&useFrame, 20, 20);
}
else
{
theDeck->DrawCard(currentCard, useFrame, isHighlighted);
// FrameRoundRect(&useFrame, 20, 20);
}
}
// SetClip( saveClipRgn ); /* restore previous value */
// DisposeRgn( saveClipRgn ); /* not needed any more */
// DisposeRgn( ourRectRgn ); /* not needed any more */
}
void WellStack::InitCards(void)
{
// done when initializing only
short i, k;
long thePaneID = GetPaneID();
CardStruct theTempCard;
// PlayingCard **currentCard;
PlayingCard *currentCard;
// currentCard = (PlayingCard **) NewHandle(4L);
theDoc->theStacks[theDoc->currentStack] = this;
if (itsCards != nil) // we're restarting a game╔
{
k = itsCards->GetCount();
for (i=1;i<=k; i++)
{
itsCards->FetchItemAt(i, ¤tCard);
delete currentCard;
}
delete itsCards;
}
// itsCards = new LDynamicArray(4L);
itsCards = new LDynamicArray(4L);
mFrameSize.height -= kStubHeight;
for (i=(thePaneID - 3001); i < NumCards; i += 8)
{
theTempCard.card = kNoCard;
currentCard = new PlayingCard(&theTempCard /*, this */);
k = (short) theDeck->theCardPositions[i];
theDeck->GetCardInfo(k, currentCard);
// tempCard->itsOwner = this;
// itsCard = tempCard;
// itsCards->InsertItemsAt(1, 9999, &itsCard);
AddCardToWell(nil, currentCard);
// whichCard = k;
}
theDoc->currentStack++;
// DrawSelf();
// DisposHandle((Handle)currentCard);
// Refresh();
// UpdatePort();
}
Boolean WellStack::CanDropOnSlot(PlayingCard *draggedCard)
{
// stack behavior is different: we need a card of an opposite color
// and an immediately lower value.
if ((draggedCard->color != itsCard->color) &&
(draggedCard->card == (itsCard->card -1)))
return (true);
else
return (false);
}
void WellStack::AddCardToWell(CardWell *whichWell, PlayingCard *whichCard)
{
whichCard->itsOwner = this;
itsCards->InsertItemsAt(1, 9999, &whichCard);
itsCard = whichCard;
AdjustFrame();
}
void WellStack::RemoveCardFromWell(CardWell *whichWell, PlayingCard *whichCard)
{
short i, maxItems;
PlayingCard *currentCard;
// currentCard = (PlayingCard **) NewHandle(4L);
// (*tempCard)->card = kNoCard;
maxItems = (short) itsCards->GetCount();
for (i = maxItems; i > 0; i--)
{
itsCards->FetchItemAt(i, ¤tCard);
if (((currentCard)->suit == whichCard->suit) &&
((currentCard)->card == whichCard->card)) // right card
{
itsCards->RemoveItemsAt(1, i);
if (i == maxItems) // need to update "top" card
{
maxItems = (short) itsCards->GetCount();
if (maxItems == 0)
{
itsCard = nil;
}
else
itsCards->FetchItemAt(maxItems, &itsCard);
}
AdjustFrame();
break; // exit for loop
}
}
// DisposHandle((Handle)currentCard);
}
void WellStack::AdjustFrame(void)
{
short maxItems, difference;
Rect frame;
RgnHandle newRgn, oldRgn, saveClipRgn;
oldRgn = NewRgn();
OpenRgn();
CalcPortFrameRect(frame);
FrameRoundRect(&frame, 20, 20);
CloseRgn(oldRgn);
newRgn = NewRgn();
difference = mFrameSize.height;
if (itsCards == nil)
maxItems = 0;
else
maxItems = (short) itsCards->GetCount();
mFrameSize.height = (theDeck->GetCardHeight()) - kStubHeight + (kStubHeight * maxItems);
difference -= mFrameSize.height;
CalcPortFrameRect(frame);
OpenRgn();
FrameRoundRect(&frame, 20, 20);
CloseRgn(newRgn);
CopyRgn(newRgn, itsCardRgn);
if (difference > 0)
{
// we need to clean up
// DiffRgn(newRgn,oldRgn,newRgn);
DiffRgn(oldRgn, newRgn, newRgn);
saveClipRgn = NewRgn();
GetClip(saveClipRgn);
SetClip(newRgn);
EraseRgn(newRgn);
SetClip(saveClipRgn);
DisposeRgn(saveClipRgn);
}
DisposeRgn(newRgn);
DisposeRgn(oldRgn);
}